﻿using Contract.Domain;
using Contract.Infrastructure.Repository;
using Contract.Read_WebApi.Application.Commands;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;

namespace Contract.Read_WebApi.Application.CommandHandler
{
    public class SubmitListCYContractCommandHandler : IRequestHandler<SubmitListCYContractCommands, PageResult<Domain.ContractDto>>
    {
        private readonly IRepository<Domain.Contract> _repository;
        private readonly Cross_services_Helper cross_Services_Helper;

        public SubmitListCYContractCommandHandler(IRepository<Domain.Contract> repository, Cross_services_Helper cross_Services_Helper)
        {
            _repository = repository;
            this.cross_Services_Helper = cross_Services_Helper;
        }

        public async Task<PageResult<ContractDto>> Handle(SubmitListCYContractCommands request, CancellationToken cancellationToken)
        {
            PageResult<Domain.ContractDto> pageResult = new PageResult<Domain.ContractDto>();
            var list = await _repository.GetAllAsync();
            var res = await cross_Services_Helper.Get($"http://10.31.60.9:8000/LongLine/api/LineInfo/GetLineRoute");
            var line = JsonConvert.DeserializeObject<List<LineDto>>(res);
            var info = from A in list.ToList()
                       join B in line on A.Route equals B.Id
                       where A.IsDel == false && A.ContractType == 2 & A.AuditState != 0
                       select new ContractDto
                       {
                           ContractId = A.ContractId,
                           ContractNumber = A.ContractNumber,
                           ContractTitle = A.ContractTitle,
                           ShipperEnterprise = A.ShipperEnterprise,
                           ShipperPrincipal = A.ShipperPrincipal,
                           Route = A.Route,
                           RouteName = B.Name,
                           TonTransport = A.TonTransport,
                           Tonnage = A.Tonnage,
                           CharterAmount = A.CharterAmount,
                           SigningDate = A.SigningDate,
                           Responsible = A.Responsible,
                           CreateDate = A.CreateDate,
                           AuditState = A.AuditState,
                           Approver = A.Approver,
                       };
            if (!string.IsNullOrEmpty(request.ContractTitle))
            {
                info = info.Where(x => x.ContractTitle.Contains(request.ContractTitle));
            }
            if (!string.IsNullOrEmpty(request.ShipperEnterprise))
            {
                info = info.Where(x => x.ShipperEnterprise.Contains(request.ShipperEnterprise));
            }
            if (!string.IsNullOrEmpty(request.ShipperPrincipal))
            {
                info = info.Where(x => x.ShipperPrincipal.Contains(request.ShipperPrincipal));
            }
            if (!string.IsNullOrEmpty(request.Responsible))
            {
                info = info.Where(x => x.Responsible.Contains(request.Responsible));
            }
            if (request.SigningDate != null)
            {
                info = info.Where(x => x.SigningDate.Equals(request.SigningDate));
            }
            if (request.State != null)
            {
                info = info.Where(x => x.AuditState.Equals(request.State));
            }
            int totalCount = info.Count();
            int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            var lists = info.OrderByDescending(x => x.CreateDate).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            pageResult.List = lists.ToList();
            pageResult.PageCount = totalPage;
            pageResult.TotalCount = totalCount;
            return pageResult;
        }
    }
}
